<?php
/**
 * Created by PhpStorm.
 * User: lhuan
 * Date: 2019/11/7
 * Time: 20:08
 */
namespace app\index\controller;

use app\index\model\Article;
use app\index\model\ArticleClassify;
use app\index\model\SysUser;
use think\Controller;

class Articles extends Index
{
    public function index()
    {
        $model = new Article();
        $model->map = [
            'status' => 1,
        ];
        foreach(input('param.search/a',[]) as $k => $v){
            if($v){
                if($k == 'pid'){
                    $model->map[$k] = $v;
                }else{
                    $model->map[$k] = ['like', '%'.$v.'%'];
                }
            }
        }
//        print_r($model->map);die;
        $model->limit = input('param.limit',15);
        $res = $model->getList();
        $total = $model->count();
        return ajax_return( 200 , 'successful' , [
            'list' => $res,
            'total' => $total
        ] );
    }

    public function getOne()
    {
        $model = new Article();
        $id = input('param.id',false);

        if(empty($id)){
            return ajax_return(201,'error');
        }else{
            return ajax_return(200,'success',$model->where(['id'=> $id])->find());
        }
    }

    public function add()
    {
        $add = Article::add(input('param.'));
        if($add){
            return ajax_return(200,'success');
        }else{
            return ajax_return(201,'error');
        }
    }

    public function modify()
    {
        $modify = Article::modify(input('param.'));
        if($modify){
            return ajax_return(200,'success');
        }else{
            return ajax_return(201,'error');
        }
    }

    public function remove()
    {
        $modify = Article::remove(input('param.id',false));
        if($modify){
            return ajax_return(200,'success');
        }else{
            return ajax_return(201,'error');
        }
    }

    public function classify()
    {
        return ajax_return(200,'success',ArticleClassify::getAllClassify());
    }
}